﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Fcdbas.Core.Reflection;

namespace System
{
    public static class DataContractExtensionMethods
    {
        private static AssemblyX assemblyX;
        
        public static System.Data.Objects.DataClasses.EntityObject ConverToModel<Entity>(this object obj,string type) where Entity : System.Data.Objects.DataClasses.EntityObject
        {
            if (assemblyX == null)
            {
                Assembly asmb = Assembly.LoadFrom("TOS.Model.dll");
                assemblyX = AssemblyX.Create(asmb);

            }
            Type t = assemblyX.GetType(type);
            Type dt = obj.GetType();
            var et = TypeX.CreateInstance(t);
           // TypeX.
            var piList = dt.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
         //   Assembly assembly = Assembly.GetAssembly(typeof(T));
          //  object resultObj = assembly.CreateInstance(string.Join(".", new string[] { typeof(Entity).Namespace, typeof(Entity).Name }));
            var piResultObj = et.GetType().GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                var sourcePi = piResultObj.Single(p => p.Name == pi.Name);
                if (sourcePi != null)
                {
                    PropertyInfoX pinfo = PropertyInfoX.Create(pi);
                    object value = pinfo.GetValue(obj);
                    PropertyInfoX spinfo = PropertyInfoX.Create(sourcePi);
                    spinfo.SetValue(et, value);
                }
            }
            return et as System.Data.Objects.DataClasses.EntityObject;
        }
        /// <summary>
        /// 实体对象间转换
        /// </summary>
        /// <typeparam name="Entity"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Entity ConverToDataContract<Entity>(this System.Data.Objects.DataClasses.EntityObject obj) where Entity : class
        {
            Type dt = obj.GetType();
            Entity et = TypeX.CreateInstance(typeof(Entity)) as Entity;
            if (obj == null || et == null)
                return et;
            // TypeX.
            var piList = dt.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            //   Assembly assembly = Assembly.GetAssembly(typeof(T));
            //  object resultObj = assembly.CreateInstance(string.Join(".", new string[] { typeof(Entity).Namespace, typeof(Entity).Name }));
            var piResultObj = et.GetType().GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                if (pi.Name == "EntityKey" || pi.Name == "EntityState")
                    continue;
                var sourcePi = piResultObj.SingleOrDefault(p => p.Name == pi.Name);
                if (sourcePi != null)
                {
                    PropertyInfoX pinfo = PropertyInfoX.Create(pi);
                    object value = pinfo.GetValue(obj);
                    PropertyInfoX spinfo = PropertyInfoX.Create(sourcePi);
                    spinfo.SetValue(et, value);
                }
            }
            return et ;
        }
    }
}
